#coding=utf8
import sys, MangaBase, re, urllib2, urlparse
from cmrhelper import GbkToUtf8, UtfToGbk

jsre = re.compile("var total=(\d+?);var volpic='(.*?)';var tpf = (\d+?);var tpf2 = (\d+?);")

tpldic = {
    1:'http://pic.tuku.cc/',
    2:'http://pic2.tuku.cc/',
    3:'http://pic3.tuku.cc/',
    8:'http://pic8.tuku.cc/',
    99:'http://pic99.tuku.cc/',
    1:'http://pic.tuku.cc/',
    2:'http://pic2.tuku.cc/',
    3:'http://pic3.tuku.cc/',
    8:'http://pic8.tuku.cc/8',
    10:'http://pic10.tuku.cc/',
    4:'http://pic4.tuku.cc/',
    7:'http://pic7.tuku.cc/',
    20:'http://tuku.cc/',
    9:'http://pic9.tuku.cc/'
    }

class Parser(MangaBase.CMangaBase):
    "89890 parser"

    def IniRe(self):
        strnamere = UtfToGbk("<title>(.*?)</title>")
        self.namere = re.compile(strnamere)

    def ParseName(self):
        "get manga name if name is None"
        self.m_mangalog.writeLog('get manga name')
        if self.namere.findall(self.chaptercontent):
            self.m_manganame = GbkToUtf8(self.namere.findall(self.chaptercontent)[0])
            self.m_mangalog.writeLog('get manga name success, manga name [%s]' %\
                                         self.m_manganame)

    def GetChapters(self):
        "parse each chapter to generate url of pictures"
        myre = re.compile("<a target=_blank href = '(\d+?)'>(.*?)</a>")
        chapterlist = []
        try:
            for item in myre.findall(self.chaptercontent):
                curl = urllib2.urlparse.urljoin(self.m_url, item[0])
                title = GbkToUtf8(item[1])
                self.m_chapterlist.append([curl, title])
        except  Exception, e:
            self.m_mangalog.writeLog(str(e)+' in getchapter', 'error')

    def ParseChapter(self, curl, title):
        "parse each chapter to generate url of pictures"
        try:
            html = urllib2.urlopen(urllib2.urlparse.urljoin(curl + '/', 'index.js')).read()
            result = jsre.findall(html)
            if not result:
                self.m_mangalog.writeLog('can not get the index.js', 'error')
                return
            total = int(result[0][0])
            volpic = result[0][1]
            tpf = int(result[0][2])
            tpf2 = int(result[0][3])
            
            if not tpldic.has_key(tpf2):
                self.m_mangalog.writeLog('cannot find host', 'error')
                return
            host = tpldic[tpf2]
            
            for url in generate_url(host, volpic, total, tpf):
                self.PushPic([url, url, title, self.m_manganame])
        except  Exception, e:
            self.m_mangalog.writeLog(str(e) + ' in getpiclist', 'error')

            
def generate_url(host, path, maxpage, length):
    picurls = []
    length += 1
    for page in range(1, maxpage + 1):
        tp = str(page * 18527)
        if len(tp) < length:
            tp = (length - len(tp)) * '0' + tp
        ptp = (length - len(str(page))) * '0' + str(page)
        url = host + path + tp + ptp + '.jpg'
        picurls.append(url)
    return picurls
